package com.android.nearshop.app.activity.map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.nearshop.R;
import com.android.nearshop.app.adapter.map.PoiOverlay;
import com.android.nearshop.app.adapter.map.PoiOverlay.OnLocSelListener;
import com.android.nearshop.app.widget.TitleBar;
import com.android.nearshop.business.location.BaseSearchListenerImpl;
import com.android.nearshop.business.location.MyBMapManager;
import com.android.nearshop.business.model.Addr;
import com.android.nearshop.util.Constant;
import com.android.nearshop.util.Util;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKSearch;


/*********************************************************
 * @author : zhaohua
 * @version : 2012-8-13
 * @see : 地图选取一点
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class SelLocActivity extends MainMapActivity 
implements OnClickListener,OnLocSelListener
{
    public static final int CODE_REQUEST = 0x8801;
    
    private TextView address_tx;
    private ImageView   btn_ok;
    private View     sure_lay;
    
    private PoiOverlay selectOverLay;
    private GeoPoint selectedGeo;
    private MKSearch mKSearch;
    private MKAddrInfo mMyAddress;
    
    @Override
    protected void onCreate(Bundle arg0)
    {
        super.onCreate(arg0);
        setContentView(R.layout.activity_locsel);
        
        address_tx = (TextView)findViewById(R.id.address_tx);
        btn_ok = (ImageView)findViewById(R.id.btn_ok); 
        sure_lay = findViewById(R.id.sure_lay);
        btn_ok.setOnClickListener(this);
        sure_lay.setOnClickListener(this);
        
        mKSearch = new MKSearch();
        mKSearch.init(MyBMapManager.getInstace(this).getBMapManager(), mSearchListener);
        
        showMyPosition(true);
        startSelLocation();
    }
    
    @Override
    protected void setupTitleBar(TitleBar titleBar)
    {
        titleBar.setTitle(R.string.title_selloc);
        titleBar.setRightVisible(false);
    }

    @Override
    public void onClick(View v)
    {
        switch(v.getId())
        {
            case R.id.btn_ok:
            case R.id.sure_lay:
            {
                onOK();
                break;
            }
        }
    }
    
    public void startSelLocation()
    {
        if(selectOverLay == null)
        {
            selectOverLay = new PoiOverlay(this);
        }
        selectOverLay.setOnLocSelListener(this);
        if( !mMapView.getOverlays().contains(selectOverLay))
        {
            mMapView.getOverlays().add(selectOverLay);
        }
    }
    /** 停止选取兴趣点*/
    public void stopSelLocation()
    {
        selectOverLay.setOnLocSelListener(null);
        mMapView.getOverlays().remove(selectOverLay);
        mMapView.postInvalidate();
        selectOverLay = null;
    }

    private void onOK()
    {
        if(selectedGeo != null)
        {
            Addr pos = new Addr();
            pos.longitude = selectedGeo.getLongitudeE6()/1E6f;
            pos.latitude = selectedGeo.getLatitudeE6()/1E6f;
            pos.address = mMyAddress !=null ? mMyAddress.strAddr : null;
            pos.city = mMyAddress !=null ? mMyAddress.addressComponents.city : null;
            pos.district = mMyAddress !=null ? mMyAddress.addressComponents.district : null;
            Intent intent = new Intent();
            intent.putExtra(Constant.TAG_ADDR, pos);
            setResult(RESULT_OK, intent);
            finish();
        }else
        {
            Util.showToast(this, R.string.no_selloc);
        }
    }
    
    @Override
    public void onBackPressed()
    {
        setResult(RESULT_CANCELED, null);
        super.onBackPressed();
    }
    
    @Override
    public void onSelect(final GeoPoint geo)
    {
         runOnUiThread(new Runnable()
        {          
            @Override
            public void run()
            {
                selectedGeo = geo;
                address_tx.setText(R.string.finding_city);
                sure_lay.setVisibility(View.VISIBLE);
                mKSearch.reverseGeocode(selectedGeo);                
            }
        });       
    }
    
    private BaseSearchListenerImpl mSearchListener = new BaseSearchListenerImpl()
    {
        @Override
        public void onGetAddrResult(MKAddrInfo paramMKAddrInfo,int paramInt)
        {
            if (paramInt != 0)
            {
                mMyAddress = null;
                address_tx.setText(R.string.can_not_find_city);
            } else
            {
                mMyAddress = paramMKAddrInfo;
                address_tx.setText(mMyAddress.strAddr);
                sure_lay.setVisibility(View.VISIBLE);
            }
        }
    };
    
    public static void startActivityResult(Activity context)
    {        
        context.startActivityForResult(getStartActivity(context), CODE_REQUEST);
    }
    
    public static Intent getStartActivity(Activity context)
    {        
        Intent intent = new Intent(context, SelLocActivity.class);
        return intent;
    }
}
